package com.notion.algorithms.datastructures.graph;

public class Edge implements Comparable<Edge>{
	private final Vertex vertex1;
	//vertex2 will have larger number than vertex1
	private final Vertex vertex2;
	private final int cost;

	@Override
	public int hashCode() {
		return 31*vertex1.getVertexNumber()+vertex2.getVertexNumber();
	}
	public Edge(Vertex vertex1, Vertex vertex2, int cost) {
		if(vertex1.getVertexNumber()<vertex2.getVertexNumber()){
			this.vertex1 = vertex1;
			this.vertex2 = vertex2;
		}else{
			this.vertex1 = vertex2;
			this.vertex2 = vertex1;
		}
		this.cost=cost;
	}

	public Vertex getVertex1() {
		return vertex1;
	}

	public Vertex getVertex2() {
		return vertex2;
	}

	public int getCost() {
		return cost;
	}

	public int compareTo(Edge o) {
		int result=this.getCost()-o.getCost();
		if(result==0){
			result = this.vertex1.getVertexNumber()-o.vertex1.getVertexNumber();
			if(result==0){
				result=this.vertex2.getVertexNumber()-o.vertex2.getVertexNumber();
			}
		}
		return result;
	}	
	
	@Override
	public boolean equals(Object obj) {
		boolean isEqual=false;
		if(obj instanceof Edge){
			Edge e = (Edge)obj;
			if(e.getVertex1().equals(this.getVertex1()) && 
					e.getVertex2().equals(this.getVertex2()))
				isEqual = true;
		}
		return isEqual;
	}
	
	public Vertex getOtherVertex(Vertex v){
		if(vertex1.equals(v))
			return vertex2;
		else if(vertex2.equals(v))
			return vertex1;
		else throw new IllegalArgumentException("Vertex not touched by Edge");
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Vertex1="+vertex1+";Vertex2="+vertex2+";Cost="+cost;
	}
}
